package com.guinea.skosite.web;

// Generated 2011-3-15 19:06:13 by Hibernate Tools 3.4.0.Beta1

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

import com.guinea.core.Constants;
import com.guinea.core.orm.BaseEntity;
import com.guinea.core.utils.StringUtils;
import com.guinea.core.web.StrutsAction;
import com.guinea.skoru.entity.SkoruShareholder;
import com.guinea.skoru.service.SkoruShareholderManager;

public class SkositeShareholderAction extends StrutsAction {

	private SkoruShareholderManager skoruShareholderManager;


	public SkoruShareholderManager getSkoruShareholderManager() {
		return skoruShareholderManager;
	}
	public void setSkoruShareholderManager(
			SkoruShareholderManager skoruShareholderManager) {
		this.skoruShareholderManager = skoruShareholderManager;
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String idString = request.getParameter("id");
		Long id = null;
		if(!StringUtils.isBlank(idString)){
			id = new Long(idString);
		}
		SkoruShareholder shareholder = null;
		//是否新建申请
		boolean isNew = false;
		if(id == null) isNew = true;

		if(!isNew){
			shareholder = getSkoruShareholderManager().getSkoruShareholder(id);
		}else{
			shareholder = new SkoruShareholder();
		}
		//save操作前的验证
		ActionMessages errors = form.validate(mapping, request);
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			renderText(response,errors.toString());
			return null;
		}

		if (shareholder == null) {
			renderText(response, "所编辑条目不存在");
			return null;
		}
		// 将lazyform内容绑定到object
		bindEntity(form, shareholder);
		//去乱码
		shareholder.setShareholderName(StringUtils.unescape(request.getParameter("shareholderName")));
		shareholder.setPercent(StringUtils.unescape(request.getParameter("percent")));
		shareholder.setStake(StringUtils.unescape(request.getParameter("stake")));
		// 为保存操作添加创建人更新人等信息
		BaseEntity baseEntity = addSaveData(shareholder,isNew);
		getSkoruShareholderManager().save(baseEntity);
		
		renderText(response,"ok");
		return null;
	}
	/**
	 * 标记删除单个对象的Action函数.
	 */
	public ActionForward erase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String idString = request.getParameter("id");
		Long id = null;
		if(!StringUtils.isBlank(idString)){
			id = new Long(idString);
		}
		SkoruShareholder shareholder = null;
		//如果缺少ID，则返回操作失败消息
		if (id != null) {
			shareholder = getSkoruShareholderManager().getSkoruShareholder(id);
		}else{
			renderText(response, "操作失败：未提供有效参数");
			return null;
		}
		//erase操作前的验证
		if (shareholder == null) {
			renderText(response, "所编辑条目不存在");
			return null;
		}
		// 为保存操作添加创建人更新人等信息
		BaseEntity baseEntity = addSaveData(shareholder,false);
		baseEntity.setStatus(Constants.STATUS_INVALID);
		getSkoruShareholderManager().save(baseEntity);
		
		renderText(response,"ok");
		return null;
	}
	
	/**
	 * 取得Shareholder对象
	 */
	public ActionForward getShareholder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String idString = request.getParameter("id");
		Long id = null;
		if(!StringUtils.isBlank(idString)){
			id = new Long(idString);
		}
		SkoruShareholder shareholder = null;
		//是否新建申请
		boolean isNew = false;
		if(id == null) isNew = true;

		if(!isNew){
			shareholder = getSkoruShareholderManager().getSkoruShareholder(id);
		}else{
			shareholder = new SkoruShareholder();
		}
		
		String result = "";
		result += "{";
		result += "\"id\" : \"" + (shareholder.getId() == null? "":shareholder.getId().longValue()+"") + "\"";
		result += ", \"applyId\" : \"" + (shareholder.getApplyId() == null? "":shareholder.getApplyId().longValue()+"") + "\"";
		result += ", \"typeCode\" : \"" + StringUtils.defaultString(shareholder.getTypeCode()) + "\"";
		result += ", \"shareholderName\" : \"" + StringUtils.defaultString(shareholder.getShareholderName()) + "\"";
		result += ", \"stake\" : \"" + StringUtils.defaultString(shareholder.getStake()) + "\"";
		result += ", \"percent\" : \"" + StringUtils.defaultString(shareholder.getPercent()) + "\"";
		result += "}";
		renderText(response,result);
		return null;
	}
	/**
	 * 取得SkoruShareholder对象列表
	 */
	public ActionForward getShareholderList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String applyId = request.getParameter("applyId");
		String typeCode = request.getParameter("typeCode");
		String result = "[";
		if(!StringUtils.isBlank(applyId)){
			List list = getSkoruShareholderManager().findShareholders(new Long(applyId),typeCode);
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				SkoruShareholder f = (SkoruShareholder) iter.next();
				result += "{";
				result += "\"id\" : \"" + (f.getId() == null? "":f.getId().longValue()+"") + "\"";
				result += ", \"applyId\" : \"" + (f.getApplyId() == null? "":f.getApplyId().longValue()+"") + "\"";
				result += ", \"typeCode\" : \"" + StringUtils.defaultString(f.getTypeCode()) + "\"";
				result += ", \"shareholderName\" : \"" + StringUtils.defaultString(f.getShareholderName()) + "\"";
				result += ", \"stake\" : \"" + StringUtils.defaultString(f.getStake()) + "\"";
				result += ", \"percent\" : \"" + StringUtils.defaultString(f.getPercent()) + "\"";
				result += "}";
				if(iter.hasNext()) result += ", ";
			}
		}
		result += "]";
		renderText(response,result);
		return null;
	}
	
}
